//
//  Info2ViewController.m
//  markmap
//
//  Created by 甘世清 on 2024/4/24.
//

#import "Info2ViewController.h"
#import "Info2CollectionViewCell.h"
#import "InfoModel.h"
#import "Info3ViewController.h"
@interface Info2ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic , strong)InfoModel * seletedModel;

@end

@implementation Info2ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.zx_navTitle = SQNSLocalString(@"星座");
    NSString * valuue = [Manager SQ_stringForKey:@"userinfo_switch"];
    if (![valuue isEqualToString:@"1"]) {
        return;
    }
    WS(weakSelf);
       if (!self.isEdit) {
           self.zx_navLeftBtn.hidden = YES;
           [self zx_setRightBtnWithText:SQNSLocalString(@"跳过") clickedBlock:^(ZXNavItemBtn * _Nonnull btn) {
               Info3ViewController * vc = [[Info3ViewController alloc]init];
               [weakSelf SQ_pushViewControllerFull:vc];
           }];
       }
}

- (void)SQ_UIViewForSelfView{
    UIImageView * bg = [[UIImageView alloc]init];
    bg.image = SQGetImage(@"me背景");
    bg.contentMode = UIViewContentModeScaleAspectFill;
    [self.view addSubview:bg];
    [bg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    [self.collectionView registerClass:[Info2CollectionViewCell class] forCellWithReuseIdentifier:@"Info2CollectionViewCell"];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.zx_navBar.mas_bottom);
    }];
    HeaderView * xiayibu = [[HeaderView alloc]init];
    xiayibu.image = @"按钮背景";
    if (self.isEdit) {
        xiayibu.text = SQNSLocalString(@"提交");
    } else {
        xiayibu.text = SQNSLocalString(@"下一步");
    }
    xiayibu.colorString = @"#FFFFFF";
    [xiayibu.btn addTarget:self action:@selector(SQ_tijiaoButton) forControlEvents:UIControlEventTouchUpInside];
    xiayibu.font = FONT_MEDIUM(18);
    [self.view addSubview:xiayibu];
    [xiayibu mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.mas_equalTo(SCREEN_WIDETH-110);
        make.height.mas_equalTo(48);
        make.bottom.equalTo(self.view.mas_bottom).offset(-FootHeight);
    }];
    [self SQ_Header];
}
- (void)SQ_Header{
    NSArray * array = @[@"水瓶座",@"双鱼座",@"白羊座",@"金牛座",@"双子座",@"巨蟹座",@"狮子座",@"处女座",@"天秤座",@"天蝎座",@"射手座",@"摩羯座"];
    NSArray * array1 = @[@"1.20-2.18",@"2.19-3.20",@"3.21-4.19",@"4.20-5.20",@"5.21-6.21",@"6.22-7.22",@"7.23-8.22",@"8.23-9.22",@"9.23-10.23",@"10.24-11.22",@"11.23-12.21",@"12.22-1.19"];
    for (int x = 0; x<array.count; x++) {
        InfoModel * model = [InfoModel new];
        model.name = [array objectAtIndex:x];
        model.time = [array1 objectAtIndex:x];
        
        [self.jsonArray addObject:model];
    }
    self.seletedModel = [self.jsonArray firstObject];
    self.seletedModel.isSeleted = YES;
    [self.collectionView reloadData];
    
}
-(void)SQ_tijiaoButton{
    [YJProgressHUD SQ_showMsgWithoutView:@""];
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:self.seletedModel.name forKey:@"starSign"];
    [HttpTool post:Url5 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        if (self.isEdit) {
            [self SQ_popViewVC];
        } else {
            Info3ViewController * vc = [[Info3ViewController alloc]init];
            [self SQ_pushViewControllerFull:vc];
        }
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
    
}

#pragma mark UICollectionViewDelegate,UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.jsonArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
    Info2CollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Info2CollectionViewCell" forIndexPath:indexPath];
    cell.model = [self.jsonArray objectAtIndex:indexPath.item];
     return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
   
    return CGSizeMake((SCREEN_WIDETH-40)/3.f-1, 120);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(10, 10, 0, 10);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    self.seletedModel.isSeleted = NO;
    self.seletedModel = [self.jsonArray objectAtIndex:indexPath.item];
    self.seletedModel.isSeleted = YES;
    [self.collectionView reloadData];
}
@end
